package views.ope;

import java.awt.Dimension;
import java.awt.Image;
import java.awt.event.ActionListener;
import java.awt.event.FocusListener;
import java.awt.event.MouseListener;
import java.sql.Date;
import java.text.ParseException;
import java.text.SimpleDateFormat;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFormattedTextField;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.border.EtchedBorder;
import javax.swing.text.MaskFormatter;

import models.cad.Aluno;
import models.cad.Desconto;
import models.cad.Usuario;
import models.ope.LDesconto;
import supers.GUIInternalFrame;
import supers.GUICadInterface;
import utils.GlobalDeclarations;
import views.SisDskGUI;

@SuppressWarnings("serial")
public class OpeLDeGUI extends GUIInternalFrame implements GUICadInterface{
	public JFormattedTextField tfIs;
	public JButton btNovo;
	public JButton btExcluir;
	public JButton btListar;
	public JFormattedTextField tfCodDsc;
	public JFormattedTextField tfCodAlu;
	public JButton btNDesc;
	public JButton btNAlu;
	public JLabel lbDsc;
	public JLabel lbAlu;
	public JLabel lbDsc1;
	public JFormattedTextField tfValIni;
	public JFormattedTextField tfValFim;
	
	public OpeLDeGUI() {
		setTitle("Lan\u00E7a Desconto");
		setSize(new Dimension(310, 250));
		try {
			
			JLabel lblAtiv = new JLabel("Aluno:");
			lblAtiv.setBounds(27, 119, 57, 14);
			getContentPane().add(lblAtiv);
			
			tfCodAlu = new JFormattedTextField(new MaskFormatter("###"));
			tfCodAlu.setColumns(3);
			tfCodAlu.setBounds(83, 116, 30, 20);
			getContentPane().add(tfCodAlu);
			
			lbAlu = new JLabel(".");
			lbAlu.setBounds(149, 119, 100, 14);
			getContentPane().add(lbAlu);
			
			JLabel lblDesconto = new JLabel("Desconto:");
			lblDesconto.setBounds(27, 64, 57, 14);
			getContentPane().add(lblDesconto);
			
			tfCodDsc = new JFormattedTextField(new MaskFormatter("###"));
			tfCodDsc.setColumns(3);
			tfCodDsc.setBounds(83, 61, 30, 20);
			getContentPane().add(tfCodDsc);
			
			lbDsc = new JLabel(".");
			lbDsc.setBounds(149, 64, 100, 14);
			getContentPane().add(lbDsc);
			
			btNAlu = new JButton("N");
			btNAlu.setBounds(115, 115, 23, 23);
			getContentPane().add(btNAlu);
			
			btNDesc = new JButton("N");
			btNDesc.setBounds(115, 60, 23, 23);
			getContentPane().add(btNDesc);
			
			JPanel panel_1 = new JPanel();
			panel_1.setBorder(new EtchedBorder(EtchedBorder.LOWERED, null, null));
			panel_1.setBounds(14, 180, 268, 26);
			getContentPane().add(panel_1);
			panel_1.setLayout(null);

			tfIs = new JFormattedTextField(new MaskFormatter("###"));
			tfIs.setColumns(3);
			tfIs.setBounds(83, 29, 30, 20);
			getContentPane().add(tfIs);
			
			btNovo = new JButton("Novo");
			ImageIcon imageIcon = new ImageIcon(SisDskGUI.class.getResource(GlobalDeclarations.ImgBtNovo));
			Image img = imageIcon.getImage().getScaledInstance(16, 16, Image.SCALE_SMOOTH);
			btNovo.setIcon(new ImageIcon(img));
			btNovo.setBounds(1, 1, 80, 23);
			panel_1.add(btNovo);
			
			btExcluir = new JButton("Excluir");
			btExcluir.setEnabled(false);
			ImageIcon imageIcon2 = new ImageIcon(SisDskGUI.class.getResource(GlobalDeclarations.ImgBtExcluir));
			Image img2 = imageIcon2.getImage().getScaledInstance(16, 16, Image.SCALE_SMOOTH);
			btExcluir.setIcon(new ImageIcon(img2));
			btExcluir.setBounds(91, 1, 85, 23);
			panel_1.add(btExcluir);
			
			btListar = new JButton("Listar");
			btListar.setBounds(186, 1, 80, 23);
			ImageIcon imageIcon3 = new ImageIcon(SisDskGUI.class.getResource(GlobalDeclarations.ImgBtListar));
			Image img3 = imageIcon3.getImage().getScaledInstance(16, 16, Image.SCALE_SMOOTH);
			btListar.setIcon(new ImageIcon(img3));
			panel_1.add(btListar);
			
			lbDsc1 = new JLabel(".");
			lbDsc1.setBounds(149, 89, 100, 14);
			getContentPane().add(lbDsc1);
			
			JLabel lblVlidoDe = new JLabel("V\u00E1lido de:");
			lblVlidoDe.setBounds(27, 149, 57, 14);
			getContentPane().add(lblVlidoDe);
			
			tfValIni = new JFormattedTextField(new MaskFormatter("##/##/####"));
			tfValIni.setBounds(83, 146, 65, 20);
			getContentPane().add(tfValIni);
			
			JLabel lblAt = new JLabel("at\u00E9:");
			lblAt.setBounds(155, 149, 24, 14);
			getContentPane().add(lblAt);
			
			tfValFim = new JFormattedTextField(new MaskFormatter("##/##/####"));
			tfValFim.setBounds(180, 146, 65, 20);
			getContentPane().add(tfValFim);
			
			JLabel lblInsc = new JLabel("Insc.:");
			lblInsc.setBounds(27, 32, 34, 14);
			getContentPane().add(lblInsc);
		} catch (ParseException e) {
			e.printStackTrace();
		}
	}

	@Override
	public void setListener(ActionListener ac, FocusListener fl, MouseListener ml) {
        tfIs.addFocusListener(fl);
        tfCodDsc.addFocusListener(fl);
        tfCodAlu.addFocusListener(fl);
        tfValFim.addFocusListener(fl);
        btNDesc.addActionListener(ac);
        btNAlu.addActionListener(ac);
        btNDesc.addFocusListener(fl);
        btNAlu.addFocusListener(fl);
        btNovo.addActionListener(ac);
        btExcluir.addActionListener(ac);
        btListar.addActionListener(ac);
	}

	@Override
	public void limpar() {
		tfIs.setText("");
		tfCodDsc.setText("");
		tfCodAlu.setText("");
		tfValIni.setText("");
		tfValFim.setText("");
		lbAlu.setText("");
		lbDsc.setText("");
		lbDsc1.setText("");
		btExcluir.setEnabled(false);
	}

	public void setDados(Object obj){
		LDesconto lDsc = (LDesconto) obj;
		if(lDsc.getIs() < 10)
			tfIs.setText("00" + String.valueOf(lDsc.getIs()));
		else if(lDsc.getIs() < 10)
				tfIs.setText("0" + String.valueOf(lDsc.getIs()));
			else
				tfIs.setText(String.valueOf(lDsc.getIs()));
		tfCodDsc.setText(lDsc.getDesconto().getCodigo());
		tfCodAlu.setText(lDsc.getAluno().getCodigo());
		tfValIni.setText(new SimpleDateFormat("dd/MM/yyyy").format(lDsc.getDtIni()));
		tfValFim.setText(new SimpleDateFormat("dd/MM/yyyy").format(lDsc.getDtFin()));
	}
	
	public LDesconto getDados(){
		LDesconto lDsc = new LDesconto();
		lDsc.setIs(Integer.valueOf(tfIs.getText().trim()));
		lDsc.setDesconto(Desconto.getDesconto(tfCodDsc.getText()));
		lDsc.setAluno(Aluno.getAluno(tfCodAlu.getText()));
		try {
			java.util.Date dataI = new SimpleDateFormat("dd/MM/yyyy").parse(tfValIni.getText());
			lDsc.setDtIni(new java.sql.Date(dataI.getTime()));
			java.util.Date dataF = new SimpleDateFormat("dd/MM/yyyy").parse(tfValFim.getText());
			lDsc.setDtFim(new java.sql.Date(dataF.getTime()));
		} catch (ParseException e) {
			JOptionPane.showMessageDialog(null, e.getMessage());
		}
		lDsc.setUsuario(Usuario.getUsuario(GlobalDeclarations.Is_Usuario));
		lDsc.setDtOpe(new Date(System.currentTimeMillis()));
		return lDsc;
	}

	@Override
	public JButton getBtNovo() {
		return btNovo;
	}

	@Override
	public JButton getBtExcluir() {
		return btExcluir;
	}

	@Override
	public JButton getBtListar() {
		return btListar;
	}
}
